package com.systemCapacity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.systemCapacity.common.dto.BattleTaskDto;
import com.systemCapacity.common.lang.Result;
import com.systemCapacity.entity.BattleTask;
import com.systemCapacity.service.BattlePlatformService;
import com.systemCapacity.service.BattleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author 任杰
 * @since 2023.6.15
 */
@RestController
@RequestMapping("/task")
public class BattleTaskController {

    @Autowired
    BattleTaskService battleTaskService;

    @Autowired
    BattlePlatformService battlePlatformService;

    /**
     * 分页查询作战任务
     * @param page 页码
     * @param size 页大小
     * @return 作战任务查询结果
     */
    @GetMapping("/list")
    public Result getTasks(@RequestParam(value = "page", defaultValue = "0") int page,
                           @RequestParam(value = "size", defaultValue = "20") int size) {
        IPage<BattleTask> taskIPage = battleTaskService.getTaskPage(page, size);
        return Result.succ(taskIPage);
    }

    /**
     * 保存作战任务
     * @param taskDto 作战任务
     * @return 保存结果
     */
    @PostMapping("/save")
    public Result addTask(@RequestBody BattleTaskDto taskDto) {
        if (taskDto == null || taskDto.getTaskName().isEmpty()) {
            return Result.fail("作战任务名不能为空");
        } else if(battleTaskService.taskNameExist(taskDto.getTaskName())) {
            return Result.fail("作战任务名重复，请重新输入作战任务名");
        }

        try {
            battleTaskService.saveTask(taskDto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("新增任务失败");
        }
        return Result.succ(null);
    }

    /**
     * 编辑作战任务
     * @param taskDto 作战任务
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result editTask(@RequestBody BattleTaskDto taskDto) {
        if (taskDto == null || taskDto.getTaskId().isEmpty()) {
            return Result.fail("作战任务不能为空");
        } else if(taskDto.getTaskName().isEmpty()) {
            return Result.fail("作战任务名不能为空");
        }
        QueryWrapper<BattleTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("taskName", taskDto.getTaskName());
        BattleTask battleTask = battleTaskService.getOne(queryWrapper);
        if (battleTask != null && !battleTask.getTaskId().equals(taskDto.getTaskId())) {
            return Result.fail("作战任务名重复，请重新输入作战任务名");
        }

        battleTaskService.editTask(taskDto);
        return Result.succ(null);
    }

    /**
     * 删除作战任务
     * @param taskId 作战任务Id
     * @return 删除结果
     */
    @GetMapping("/delete/{id}")
    public Result deleteTask(@PathVariable(value = "id") String taskId) {
        if (!battleTaskService.taskExist(taskId)) {
            return Result.fail("该任务不存在,请确认要删除的任务是否正确");
        }
        try {
            battleTaskService.deleteTsk(taskId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除任务失败");
        }
        return Result.succ(null);
    }
    /**
     * 查询任务详情
     * @param taskId 任务id
     * @return 查询结果
     */
    @GetMapping("/taskDetail")
    public Result getTaskDetail(String taskId) {
        return Result.succ(battleTaskService.getTaskDetail(taskId));
    }
}
